package com.ruoyi.elastic.service.impl;

import com.ruoyi.elastic.domain.SkuInfo;
import com.ruoyi.elastic.repository.SkuInfoRepos;
import com.ruoyi.elastic.service.SkuInfoReposService;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import org.slf4j.*;


@Service
public class SkuInfoReposServiceImpl implements SkuInfoReposService {

    private Logger logger = LoggerFactory.getLogger(SkuInfoReposServiceImpl.class);

    @Autowired
    private SkuInfoRepos skuInfoRepos;


    @Override
    public Map simpleQueryById(String id) {
        Map<String,Object> map = new HashMap<>();
        map.put("findById",skuInfoRepos.findById(id));
        map.put("findByPriceBetween",skuInfoRepos.findByPriceBetween("900","901"));
        return map;
    }


    @Override
    public Map complexQuerySkuInfo(Map<String, String> condition) {
        Map<String,Object> map = new HashMap<>();
        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        SearchQuery searchQuery = new NativeSearchQuery(queryBuilder).setPageable(PageRequest.of(0,100));
        logger.info("-----------------------SearchQuery 分页----------------");
        Page<SkuInfo>  skuInfoPage = skuInfoRepos.search(searchQuery);
        skuInfoPage.forEach(skuInfo ->logger.info(skuInfo.toString()));
        map.put("searchQuery1",skuInfoPage);
        return map;
    }


}
